package com.lzy.cloud.base.controller;

import com.lzy.cloud.base.dto.BaseAreaDto;
import com.lzy.cloud.base.dto.BaseAreaRecordDto;
import com.lzy.cloud.base.facade.AreaFacade;
import com.lzy.cloud.base.response.BaseAreaResponse;
import com.lzy.cloud.base.service.AreaRecordService;
import com.lzy.cloud.base.service.AreaService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/area")
@Slf4j
public class AreaController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaRecordService areaRecordService;

    @Autowired
    private AreaFacade areaFacade;

    /**
     * 获取地区列表
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(@RequestParam(value = "parentId") Long parentId) {
        List<BaseAreaDto> baseAreaDtoList = areaService.getListByParentId(parentId);
        Ensure.that(baseAreaDtoList).isNotNull("00020001");
        return new Result(DozerUtils.maps(baseAreaDtoList, BaseAreaResponse.class));
    }

    /**
     * 获取详情
     * @param code
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public Result detail(@RequestParam(value = "code") Integer code) {
        BaseAreaRecordDto baseAreaRecordDto = areaRecordService.findAreaByCode(code);
        Ensure.that(baseAreaRecordDto).isNotNull("00020002");
        BaseAreaDto baseAreaDto = areaService.findById(baseAreaRecordDto.getAreaId());
        Ensure.that(baseAreaDto).isNotNull("00020003");
        return new Result(DozerUtils.map(baseAreaDto, BaseAreaResponse.class));
    }

    /**
     * 新增
     * @param baseAreaResponse
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody BaseAreaResponse baseAreaResponse) {
        Ensure.that(baseAreaResponse.getName()).isNotNull("00020005");
        BaseAreaDto baseAreaDto = DozerUtils.map(baseAreaResponse,BaseAreaDto.class);
        BaseAreaRecordDto baseAreaRecordDto = areaRecordService.findAreaByCode(baseAreaDto.getCityCode());
        Ensure.that(baseAreaRecordDto).isNull("00020006");
        Boolean flag = areaFacade.addArea(baseAreaDto);
        return new Result(flag);
    }

    /**
     * 修改
     * @param baseAreaResponse
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody BaseAreaResponse baseAreaResponse) {
        Ensure.that(baseAreaResponse.getId()).isNotNull("00020008");
        Ensure.that(baseAreaResponse.getName()).isNotNull("00020005");
        BaseAreaDto baseAreaDto = DozerUtils.map(baseAreaResponse,BaseAreaDto.class);
        Boolean flag = areaFacade.updateArea(baseAreaDto);
        return new Result(flag);
    }


}